<?php

class UserController extends Zend_Controller_Action 
{
    
    public function indexAction(){
        $this->view->assign('name','kid');
        $this->view->assign('title','Hello');
        
        
    }
    public function  nameAction(){
        $request=  $this->getRequest();
        $this->view->assign('name',$request->getParam('username'));
        $this->view->assign('gender',$request->getParam('gender'));
        
        $this->view->assign('title','User Name');
    }
    public function registerAction(){
        $request= $this->getRequest();
        
        $this->view->assign('action','process');
        $this->view->assign('title','Member Registration');
        $this->view->assign('label_fname','First Name');
        $this->view->assign('label_lname','Last Name');
        $this->view->assign('label_uname','UserName');
        $this->view->assign('label_pass','PassWord');
        $this->view->assign('label_submit','Register');
        $this->view->assign('description','Please enter this form to complete:');
        
                
    }
    public function processAction(){
        //cach 1
      /*  $params = array(
          'host' => 'localhost',
          'username' => 'root',
           'password' => 'iktmysql',
            'dbname' => 'xsis'
        );
        
        $DB = new Zend_Db_Adapter_Pdo_Mysql($params); */
        //cach 2
        $registry = Zend_Registry::getInstance();
        $DB = $registry['connectDB'];
        $request = $this->getRequest();
        
        //$sql="Insert into user (first_name,last_name,user_name,password) values ('".$request->getParam('first_name')."','".$request->getParam('last_name')."','".$request->getParam('user_name')."',MD5('".$request->getParam('password')."')) ";
        $data = array(
          'first_name' => $request->getParam('first_name'),
          'last_name' => $request->getParam('last_name'),
           'user_name' =>$request->getParam('user_name'),
            'password' =>  md5($request->getParam('password'))
        );
        //$sql = "INSERT INTO `user`(`first_name` , `last_name` ,`user_name` ,`password`)  VALUES ('".$request->getParam('first_name')."', '".$request->getParam('last_name')."', '".$request->getParam('user_name')."', MD5('".$request->getParam('password')."'))";
        $DB->insert('user', $data);
        //$DB->query($sql);
        
        $this->view->assign('title','Registration Process');
        $this->view->assign('description','Registration Success');
    }
    public function listAction(){
       /* $params = array(
            'host' => 'localhost',
            'username' => 'root',
            'password' => 'iktmysql',
            'dbname' => 'xsis'
            );
        $DB= new Zend_Db_Adapter_Pdo_Mysql($params);*/
        
       //$DB->setFetchMode(Zend_Db::FETCH_OBJ);
      //  $registry=  Zend_Registry::getInstance();
        $DB=  Zend_Registry::get('connectDB');
       $sql = "SELECT * FROM `user` ORDER BY user_name ASC";
       $select=$DB->select()
               ->from(' `user` ')
               ->where('id=4');
               
        $result = $DB->fetchAll($sql);
       // $result = $DB->fetchAll($select);
        
        
        $this->view->assign('title','Member List');
        $this->view->assign('description','Below this is our member: ');
        $this->view->assign('datas',$result);
        
        
    }
    public function editAction(){
        $params = array(
          'host' => 'localhost',
          'username' => 'root',
          'password' => 'iktmysql',
          'dbname' => 'xsis'
        );
        $DB=new Zend_Db_Adapter_Pdo_Mysql($params);
        $request =  $this->getRequest();
        $id = $request->getParam('id');
        $sql="select * from user where id='".$id."'";
        $result=$DB->fetchRow($sql);
        
        $this->view->assign('data',$result);
        $this->view->assign('action',$request->getBaseURL()."/user/processedit");
        $this->view->assign('title','Member Editing');
        $this->view->assign('label_fname','First Name');
        $this->view->assign('label_lname','Last_Name');
        $this->view->assign('label_uname','User Name');
        $this->view->assign('label_pass','Password');
        $this->view->assign('label_submit','Edit');
        $this->view->assign('description','Please update this form complete');
        
        
        
    }
    public function processeditAction(){
        $params = array(
          'host' => 'localhost',
           'username' => 'root',
            'password' => 'iktmysql',
            'dbname' => 'xsis'
        );
       $DB=new Zend_Db_Adapter_Pdo_Mysql($params);
       $request=  $this->getRequest();
       $id = $request->getParam('id');
       
       //cách 1
       /*$sql = "update user set first_name= '".$request->getParam('first_name')."',
                               last_name='".$request->getParam('last_name')."',
                               user_name='".$request->getParam('user_name')."'
               where id='".$id."'";
       $DB->query($sql); */
       
       //cách 2
       $data = array(
         'first_name' => $request->getParam('first_name'),
           'last_name' => $request->getParam('last_name'),
           'user_name' => $request->getParam('user_name')
       );
       $DB->update('user',$data,'id='.$id);
       $this->view->assign('title','Editing Process');
       $this->view->assign('description',"Edit Successfully");
    }
    public function delAction(){
        $params = array(
          'host' => 'localhost',
            'username' => 'root',
            'password' => 'iktmysql',
            'dbname' => 'xsis'
        );
        $DB = new Zend_Db_Adapter_Pdo_Mysql($params);
        $request=  $this->getRequest();
        $id=$request->getParam('id');
        //cách 1
        /*$sql="delete from user where id='".$id."'";
        $DB->query($sql);*/
        
        //cách 2
        $DB->delete('user','id='.$id);
        
        
        $this->view->assign('title','Delete Data');
        $this->view->assign('description','Delete Data Success');
        $this->view->assign('list',$request->getBaseURL()."/user/list");
    }
           
         
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
